package com.lin.cms.service.impl;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lin.cms.common.upload.UploadUtils;
import com.lin.cms.criteria.UploadCriteria;
import com.lin.cms.dao.UploadMapper;
import com.lin.cms.domain.Upload;
import com.lin.cms.service.UploadService;
import com.lin.cms.util.Paging;

@Service
public class UploadServiceImpl implements UploadService{
	
	@Autowired
    private UploadMapper uploadMapper;
    
	public Upload selectByPrimaryKey(String filename){
		return uploadMapper.selectByPrimaryKey(filename);
	}
	
	public List<Upload> selectByCriteria(UploadCriteria criteria){
		return uploadMapper.selectByCriteria(criteria);
	}
	
	public int deleteByPrimaryKey(String filename){
		int actualRowsAffected = 0;
		
		Upload _oldUpload = uploadMapper.selectByPrimaryKey(filename);
		
		if (_oldUpload != null) {
			actualRowsAffected = uploadMapper.deleteByPrimaryKey(filename);
		}
		
		return actualRowsAffected;
	}
	
	public int deleteByCriteria(UploadCriteria criteria){
		int actualRowsAffected = 0;
		
		List<Upload> _oldUpload = uploadMapper.selectByCriteria(criteria);
		
		if (_oldUpload.size() != 0) {
			actualRowsAffected = uploadMapper.deleteByCriteria(criteria);
		}
		
		return actualRowsAffected;
	}
	
	public int insert(Upload upload){
		return uploadMapper.insert(upload);
	}
	
	public int updateByPrimaryKeySelective(Upload upload){
		int actualRowsAffected = 0;
		
		String advertisingId = upload.getFilename();

		Upload _oldUpload = uploadMapper.selectByPrimaryKey(advertisingId);
		
		if (_oldUpload != null) {
			actualRowsAffected = uploadMapper.updateByPrimaryKeySelective(upload);
		}
		
		return actualRowsAffected;
	}
	
	public int updateByPrimaryKey(Upload upload){
		int actualRowsAffected = 0;
		
		String advertisingId = upload.getFilename();

		Upload _oldUpload = uploadMapper.selectByPrimaryKey(advertisingId);
		
		if (_oldUpload != null) {
			actualRowsAffected = uploadMapper.updateByPrimaryKey(upload);
		}
		
		return actualRowsAffected;
	}
	
	public List<Upload> selectByPage(UploadCriteria criteria) {
		int totalResults = uploadMapper.countByCriteria(criteria);
		Paging paging = criteria.getPaging();
		paging.setTotalResults(totalResults);
		
		return uploadMapper.selectByPage(criteria,new RowBounds(paging.getFirstResult(),paging.getMaxResults()));
	}
	
	public int countByCriteria(UploadCriteria criteria){
		return uploadMapper.countByCriteria(criteria);
	}

	public String storeByFilename(String filename, InputStream in)
			throws IOException {
		// TODO Auto-generated method stub
		Upload file = uploadMapper.selectByPrimaryKey(filename);
		if (file != null) {
			update(file, in);
		} else {
			save(filename, in);
		}
		return null;
	}
	private Upload update(Upload file, InputStream in) throws IOException {
		byte[] content = IOUtils.toByteArray(in);
		file.setContent(content);
		file.setLastModified(System.currentTimeMillis());
		file.setLength(content.length);
		in.close();
		return file;
	}
	
	private Upload save(String filename, InputStream in) throws IOException {
		byte[] content = IOUtils.toByteArray(in);
		Upload file = new Upload(filename, content.length, System.currentTimeMillis(), content);
		uploadMapper.insert(file);
		in.close();
		return file;
	}

	public String storeByExt(String path, String ext, InputStream in)
			throws IOException {
		// TODO Auto-generated method stub
		String filename;
		Upload file;
		// 判断文件是否存在
		do {
			filename = UploadUtils.generateFilename(path, ext);
			file = selectByPrimaryKey(filename);
		} while (file != null);
		save(filename, in);
		return filename;
	}
	
}
